package com.ahwei.controller;

import com.ahwei.pojo.User;
import com.ahwei.service.impl.UserServiceImpl;
import com.ahwei.util.LogUtils;
import com.ahwei.util.anno.Controller;
import com.ahwei.util.anno.FunctionMapping;
import com.ahwei.util.anno.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author：ahwei163@qq.com
 * @date: 202*-03-15 16:06:32
 * @description: 这是一个Java文件！
 */
@Controller
public class PageController {
    @FunctionMapping("验证cookie")
    @RequestMapping("/checkCookie")
    public String checkCookie(HttpServletRequest request, HttpSession session) {
        if (session.getAttribute("currentUser") == null) {
            LogUtils.info("验证cookie中...");
            //判断cookie里是否有
            Cookie[] cookies = request.getCookies();
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    if ("account".equals(cookie.getName())) {
                        //获取用户信息
                        User user = new UserServiceImpl().findUserByAccount(cookie.getValue());

                        LogUtils.info("user = " + user);
                        session.setAttribute("currentUser", user);
                    }
                }
            }
        }
        return "redirect:showIndex";
    }

    @FunctionMapping("进入主页")
    @RequestMapping("/showIndex")
    public String showIndex(HttpServletRequest request) {

        return "main.html";
    }

    @FunctionMapping("进入视频详情")
    @RequestMapping("/showVideoDetail")
    public String showVideoDetail(long vid, HttpSession session) {
        //获取session中的用户
        User currentUser = (User) session.getAttribute("currentUser");
        if (currentUser == null) {
            return "redirect:login.html";
        }
        session.setAttribute("vid", vid);
        //判断角色、视频类型
        return "redirect:html/videoDetail.html";
    }
}
